package com.itheima.reggie.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.reggie.common.BaseContext;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.PageDto;
import com.itheima.reggie.entity.Employee;
import com.itheima.reggie.service.EmployeeService;
import com.itheima.reggie.utils.IdWorker;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequestMapping("/employee")
@Slf4j
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;

    @Autowired
    private IdWorker idWorker;

    @PostMapping("/login")
    public R login(@RequestBody Employee employee, HttpSession session) {
        String userName = employee.getUsername();
//        根据用户名和密码查询.判断用户是否存在

        Employee employeeInDb = employeeService.getEmployeeByName(userName);
        if (employeeInDb == null) {
            return R.error("用户名或密码错误");
        }
        String pwdMD5Str = DigestUtils.md5DigestAsHex(employee.getPassword().getBytes());
        String passwordInDb = employeeInDb.getPassword();
        if (!pwdMD5Str.equals(passwordInDb)) {
            return R.error("用户名或密码错误");
        }

        if (employeeInDb.getStatus() == 0) {
            return R.error("该用户已被禁用");
        }

//        用户名密码均验证成功,将用户信息存储到session,登录放行成功
        session.setAttribute("employeeId", employeeInDb.getId());
        return R.success(employeeInDb);
    }

    @PostMapping("/logout")
    public R<String> logout(HttpSession session) {
        session.invalidate();
        return R.success("退出成功");
    }

    @GetMapping("/page")
    public R pages(@RequestParam int page, @RequestParam int pageSize, @RequestParam(required = false) String name) {
        PageHelper.startPage(page, pageSize);
        List<Employee> list = employeeService.getList(name);
        PageInfo pageInfo = new PageInfo(list);
        PageDto pageDto = new PageDto();
        BeanUtils.copyProperties(pageInfo, pageDto, "records");
        pageDto.setRecords(pageInfo.getList());
        log.info("list {}", list);
        return R.success(pageDto);
    }


//    添加员工
    @PostMapping
    public R add(@RequestBody Employee employee){
//      数据填充

        employee.setStatus(1); //默认开启
        employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes())); //密码默认123456
        employee.setCreateTime(LocalDateTime.now());
        employee.setUpdateTime(LocalDateTime.now());
        employee.setCreateUser((Long) BaseContext.getCurrentData());
        employee.setUpdateUser((Long) BaseContext.getCurrentData());
        employee.setId(idWorker.nextId());

        employeeService.add(employee);
        return R.success("新增成功");
    }

//    编辑员工
//   根据id查询
    @GetMapping("/{id}")
    public R getById(@PathVariable("id") Long id){
        Employee employee = employeeService.getById(id);
        return R.success(employee);
    }

//   修改员工
    @PutMapping
    public R update(@RequestBody Employee employee){
        employee.setUpdateUser((Long) BaseContext.getCurrentData());
        employee.setUpdateTime(LocalDateTime.now());
        employeeService.update(employee);
        return R.success("修改成功");
    }
}
